package com.books.controller.login_regis;/*
 *@Auther Zhourui
 *@data 2021/9/28  16:33
 *@Email 1605576647@qq.com
 */

import com.books.model.bo.Result;
import com.books.model.entity.common.People;
import com.books.model.entity.login_regis.UserLogin;
import com.books.service.login_regis.IUserLoginService;
import com.books.service.people.IPeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;


@Controller
@RequestMapping(value = "/users",method= {RequestMethod.GET,RequestMethod.POST})
public class VerifyloginController {
    @Autowired(required = false)
    private UserLogin userLogin;

    @Autowired
    private IPeopleService peopleService;

    @Autowired(required = false)
    private IUserLoginService userLoginService;

    @Autowired(required = false)
    private JavaMailSender mailSender;

    @Autowired(required = false)
    private RedisTemplate redisTemplate;

    @GetMapping("/send")
    @ResponseBody
    public Result send(HttpServletRequest request,String email){
        SimpleMailMessage message=new SimpleMailMessage();
        userLogin=userLoginService.loginByEmail(email);
        message.setFrom("1605576647@qq.com");
        message.setSubject("云上书屋");
        int n = (int) (Math.random()*90000+10000);
        //将这个验证码放入到session - 不能设置过期时间
        //以后放到redis,设置过期时间
        HttpSession session = request.getSession();
       // session.setAttribute("code",String.valueOf(n));
        redisTemplate.opsForValue().set("code",String.valueOf(n),60, TimeUnit.SECONDS);
        message.setText("验证码："+n);
        message.setTo(email);
        mailSender.send(message);
        session.setAttribute("user",userLogin);
        return new Result("200","邮箱发送成功");
    }

    @PostMapping("/get")
    @ResponseBody
    public Result get(HttpServletRequest request,String code,String account) {
        if (null == code || code.trim().length() == 0) {
            return new Result("400", "验证码不能为空!");
        }
        HttpSession session = request.getSession();
        String sesCode = (String) redisTemplate.opsForValue().get("code");
        //如果验证码放入到redis,可能是失效的.过期的
        if (sesCode==null||sesCode.equals(null)) {
            return new Result("500", "验证码过期了!");
        }
        if (code.equals(sesCode)) {
            session.setAttribute("people",peopleService.findByEmail(account));
            return new Result("200", "登录成功!");
        }
        if (!code.equals(sesCode)) {
            return new Result("505", "验证码错误!");
        }
        return null;
    }
    @GetMapping("/exit")
    public String exit(HttpServletRequest request){
        HttpSession session=request.getSession();
        //清空session
        session.invalidate();
        return "redirect:/index01";
    }

    @GetMapping("/quire")
    @ResponseBody
    public Result ifemail(String account){
        System.out.println(userLoginService.loginByEmail(account));
            if(userLoginService.loginByEmail(account)!=null){
                return new Result("200","邮箱存在！");
            }
        return new Result("404","邮箱不存在!");
    }
}
